package com.yu.demo.user.domain.valueobject;

import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
 * 用户角色值对象
 * 封装用户角色的业务规则和验证逻辑
 */
@Getter
@EqualsAndHashCode
public class UserRole {
    public static final UserRole ADMIN = new UserRole("ADMIN");
    public static final UserRole USER = new UserRole("USER");
    public static final UserRole GUEST = new UserRole("GUEST");

    private final String value;

    public UserRole(String value) {
        if (value == null || value.trim().isEmpty()) {
            throw new IllegalArgumentException("用户角色不能为空");
        }
        String normalizedValue = value.trim().toUpperCase();
        if (!isValidRole(normalizedValue)) {
            throw new IllegalArgumentException("无效的用户角色: " + value);
        }
        this.value = normalizedValue;
    }

    private boolean isValidRole(String role) {
        return "ADMIN".equals(role) || "USER".equals(role) || "GUEST".equals(role);
    }

    public boolean isAdmin() {
        return "ADMIN".equals(value);
    }

    public boolean isUser() {
        return "USER".equals(value);
    }

    public boolean isGuest() {
        return "GUEST".equals(value);
    }

    @Override
    public String toString() {
        return value;
    }
}
